<?php 
require_once("DB.php");
class Shop_Product{
	/**
	 * 
	 * The name of the product
	 * @var string
	 */
	private $id = 0;
	private $title ;
	private $producerMainName;
	private $producerFirstName ;
	protected $price = 0;
	private $discount = 0;
	
	function __construct($title="default product",$mainName="last name",$firstName ="first name",$price=0)
	{
		// what if the user enters a function name that isn't correct
		// for example $lastName instead of $mainName? how do we check?
		// look at interceptor methods
		$this->title = $title;
		$this->producerMainName =$mainName;
		$this->producerFirstName = $firstName;
		$this->price = $price;	
	}
	function getDiscount($num){
		return $this->discount;
	}
	function setDiscount($num){
		$this->discount = $num;
	}
	function getPrice(){
		return ($this->price - $this-discount);
	}
	function getTitle(){
		return $this->title;
	}
	function getProducer(){
		// Here we use the complex syntax wrapping content of strings in {}
		
		return "{$this->producerFirstName}"." {$this->producerMainName}";
	}
	function getSummaryLine(){
		$base = "{$this->title} ( {$this->producerMainName}, ";
		$base .= "{$this->producerFirstName} )";
		$base .= ": page count - {$this->playLength}";
		return $base;
		
	}
	public function setID($id){
		$this->id = $id;
	}
	public static function getInstance($id,DB_Common $db){
		$query = "select * from products where id ='$id'";
		$query_result = $db->query($query);
		if(DB::isError($query_result)){
			die($query_result->getMessage());
		}
		$row =$query_result->fetchRow(DB_FETCHMODE_ASSOC);
		if(empty($row)){return null;}
		if($row['type']== "book"){
			$product = new BookProduct(
								$row['title'],
								$row['firstname'],
								$row['mainname'],
								$row['price'],
								$row['numpages']
								);
		}else if ($row['type'] =='cd'){
			$product = new CDProduct(
						$row['title'],
						$row['firstname'],
						$row['mainname'],
						$row['price'],
						$row['playlength']);
			}else {
				$product = new Shop_Product(
						$row['title'],
						$row['firstname'],
						$row['mainname'],
						$row['price']
						);
			}
			$product->setId($row['id']);
			$product->setDiscount($row['discount']);
			return $product;
	}
}
?>